#include <stdio.h>
#include "circlelist.h"

struct Value
{
        CircleListNode circleNode;  //让大千世界包含小的节点 并且放在头部让其重叠
        int v;
};

int main01(int argc, char const *argv[])
{
        int ret = 0, i = 0;
        CircleList* list = NULL;

        struct Value v1,v2,v3,v4;
        
        list =  CircleList_Create();

        v1.v = 1;v2.v = 2;v3.v = 3;v4.v = 4;
        
        CircleList_Insert(list, (CircleListNode*)&v1, 0); //头插法
        CircleList_Insert(list, (CircleListNode*)&v2, 0);
        CircleList_Insert(list, (CircleListNode*)&v3, 0);
        CircleList_Insert(list, (CircleListNode*)&v4, 0);

        printf("循环打印：");
        for (i = 0; i < 2*CircleList_Length(list); i++) //怎么证明是循环链表
        {
                struct Value* pv = (struct Value*)CircleList_Get(list, i);
                printf("%d ",  pv->v);
        }
        printf("\n");
        
        printf("删除掉：");
        while (CircleList_Length(list) > 0)
        {
                struct Value* tmp = (struct Value *)CircleList_Delete(list, 0);
                printf("%d ",  tmp->v);
        }
        printf("\n");

        CircleList_Destory(list);

        return 0;
}

//循环列表解决约瑟夫问题
//传统的办法非常的麻烦,循环链表解决比较简单
void yusefu()
{
        int i = 0, ret = 0;
        CircleList* list = NULL;

        list =  CircleList_Create();

        struct Value v1, v2, v3, v4, v5, v6, v7, v8;
        v1.v = 1;       v2.v = 2;      v3.v=3;        v4.v = 4;
        v5.v = 5;       v6.v = 6;      v7.v=7;        v8.v = 8;

        ret = CircleList_Insert(list, (CircleListNode*)&v1, CircleList_Length(list)); //尾插法
        if(ret < 0){
                printf("CircleList_Insert err %d\n", ret);
                return ;
        }
        CircleList_Insert(list, (CircleListNode*)&v2, CircleList_Length(list));
        CircleList_Insert(list, (CircleListNode*)&v3, CircleList_Length(list));
        CircleList_Insert(list, (CircleListNode*)&v4, CircleList_Length(list));
        CircleList_Insert(list, (CircleListNode*)&v5, CircleList_Length(list));
        CircleList_Insert(list, (CircleListNode*)&v6, CircleList_Length(list));
        CircleList_Insert(list, (CircleListNode*)&v7, CircleList_Length(list));
        CircleList_Insert(list, (CircleListNode*)&v8, CircleList_Length(list));
        
        for (i = 0; i < CircleList_Length(list); i++)
        {
                //获取游标所指的元素, 然后游标下移
                struct Value* pv = (struct Value *)CircleList_Next(list);
                printf("%d ", pv->v);
        }
        printf("\n");

        //重置游标
        CircleList_Reset(list);

        while(CircleList_Length(list)>0)
        {
                struct Value* pv = NULL;
                for (i = 0; i < 3; i++)
                {
                        CircleList_Next(list);
                }
                
                pv = (struct Value *)CircleList_Current(list);
                printf("%d ", pv->v);
                CircleList_DeleteNode(list, (CircleListNode *)pv);
        }
        printf("\n"); 

        CircleList_Destory(list);
}

int main(int argc, char const *argv[])
{
        yusefu();
        return 0;
}
